import React, { useContext, useState } from "react";
import {
  View,
  Text,
  StyleSheet,
  TouchableOpacity,
  FlatList,
  Alert,
} from "react-native";
import TestCard from "../../components/preAdmin/TestCard";
import TestContext from "../../context/preparation/TestContext";
import NoData from "../../components/preAdmin/NoData";
import LoadingBar from "../../components/InterView/LoadingBar";

const TestSelected = ({ navigation }) => {
  const { testLists, setStatus, isTestLoading } = useContext(TestContext);
  const [selectedId, setSelectedId] = useState(null);

  const setSelectOption = (id) => {
    setSelectedId(id);
  };

  const onConfirm = () => {
    Alert.alert("提示", "你确定要发布该测试吗?\n(原测试将会被替换)", [
      {
        text: "确定",
        onPress: () => {
          if (selectedId === null) {
            Alert.alert("提示", "您未选择任何内容", [{ text: "确定" }]);
            return;
          }
          setStatus(selectedId);
        },
      },
      {
        text: "取消",
      },
    ]);
  };

  return (
    <View style={styles.container}>
      <LoadingBar visible={isTestLoading} />
      <FlatList
        style={{ margin: 10 }}
        data={testLists}
        keyExtractor={(item, index) => index.toString()}
        ListEmptyComponent={<NoData />}
        renderItem={({ item, index }) => {
          return (
            <TestCard
              test={item}
              onPress={() => {
                navigation.navigate("TestPreview", {
                  test: item,
                  title: "试卷内容",
                  type: "readOnly",
                });
              }}
              onSelected={setSelectOption}
              selectedId={selectedId}
              isSelect={true}
            />
          );
        }}
      />
      <TouchableOpacity style={styles.Button} onPress={() => onConfirm()}>
        <Text style={styles.Text}>发布</Text>
      </TouchableOpacity>
    </View>
  );
};

var styles = StyleSheet.create({
  container: {
    backgroundColor: "white",
    flex: 1,
  },
  Button: {
    borderRadius: 5,
    margin: 18,
    padding: 10,
    backgroundColor: "#3ABEF0",
    alignItems: "center",
    justifyContent: "center",
    elevation: 2, //设置阴影角度，通过这个设置有无阴影
    shadowColor: "gray", // 设置阴影色
  },
  Text: {
    fontSize: 16,
    color: "#fff",
    fontWeight: "bold",
  },
});

export default TestSelected;
